package
{
	import com.xcopy.Screen;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class Slot25Messenger extends Screen
	{
		protected var queue:Array = [];
		protected var current:Object;
		private var _table:Slot25Table;

		public function get table():Slot25Table
		{
			return _table;
		}

		public function Slot25Messenger(r:* = null)
		{
			super(r);
			_table = r as Slot25Table;
		}

		override public function set clip(c:MovieClip):void
		{
			super.clip = c;
			disable();
		}

		// 從佇列裡彈出下一個訊息
		public function pop():void
		{
			if (queue.length == 0)
			{
				disable();
				return;
			}
			current = queue.shift();
			clip.gotoAndStop('None'); // <= 先跳到 none 去清除所有的物件及事件監聽
			this.table.playSound('1030');
			switch (current.Type)
			{
				case 1: // 普通的訊息視窗，只有一個確定鈕
				{
					clip.gotoAndStop('Message');
					clip.txtContent.text = current.Content;
					setClickEvent(clip.btnOK, onMessageOK);
					setClickEvent(clip.btnOKAll, onMessageOKAll);
					clip.btnOKAll.visible = current.Other;
					break;
				}
				case 2: // 登入保留機台
				{
					clip.gotoAndStop('Login');
					setClickEvent(clip.btnOK, onLoginOK);
					setClickEvent(clip.btnClose, onMessageClose);
					break;
				}
				case 3: // 設定保留密碼
				{
					clip.gotoAndStop('Reserve');
					clip.rdoUseless.selected = true;
					setClickEvent(clip.btnLeave, onLeave);
					setClickEvent(clip.btnReserve, onReserve);
					setClickEvent(clip.btnClose, onMessageClose);
					break;
				}
				case 4: // 詢問保留機台
				{
					clip.gotoAndStop('Return');
					clip.txtContent.text = current.Content;
					setClickEvent(clip.btnOld, onOld);
					setClickEvent(clip.btnNew, onNew);
					break;
				}
			}
			enable();
		}

		public function onOld(e:MouseEvent):void
		{
			this.table.playSound('1010');
			current.OnOld();
			pop();
		}

		public function onNew(e:MouseEvent):void
		{
			this.table.playSound('1010');
			current.OnNew();
			pop();
		}

		public function onLeave(e:MouseEvent):void
		{
			this.table.playSound('1010');
			current.OnLeave(0, '');
			pop();
		}

		public function onReserve(e:MouseEvent):void
		{
			this.table.playSound('1010');
			current.OnLeave(clip.rdoUseless.selected ? 1 : 2, clip.txtPass.text);
			pop();
		}

		public function onLoginOK(e:MouseEvent):void
		{
			current.OnOK(clip.txtPass.text);
			this.table.playSound('1010');
			pop();
		}

		public function onMessageClose(e:MouseEvent):void
		{
			this.table.playSound('1020');
			pop();
		}

		public function onMessageOK(e:MouseEvent):void
		{
			this.table.playSound('1010');
			pop();
		}

		public function onMessageOKAll(e:MouseEvent):void
		{
			this.table.playSound('1010');
			var i:Number   = 0;
			var temp:Array = [];
			for (i = 0; i < queue.length; i++)
			{
				if (queue[i].Type != 1)
				{
					temp.push(queue[i]);
				}
			}
			queue = temp;
			pop();
		}

		// 壓入訊息視窗
		public function addAlert(content:String, other:Boolean = true):void
		{
			var data:Object = {Type: 1, Content: content, Other: other};
			queue.push(data);
			if (!clip.visible)
			{
				pop();
			}
		}

		// 壓入「進入機台」視窗
		public function addLogin(onOK:Function):void
		{
			var data:Object = {Type: 2, OnOK: onOK};
			queue.push(data);
			if (!clip.visible)
			{
				pop();
			}
		}

		// 壓入「保留機台」視窗
		public function addReserve(onLeave:Function):void
		{
			var data:Object = {Type: 3, OnLeave: onLeave};
			queue.push(data);
			if (!clip.visible)
			{
				pop();
			}
		}

		// 壓入「已有機台」視窗
		public function addAlready(content:String, onOld:Function, onNew:Function):void
		{
			var data:Object = {Type: 4, Content: content, OnOld: onOld, OnNew: onNew};
			queue.push(data);
			if (!clip.visible)
			{
				pop();
			}
		}
	}
}
